package com.sy.controller;

import com.sy.dto.JsonResult;
import com.sy.entity.Page;
import com.sy.service.EditService;
import com.sy.utils.StateUtils;
import com.sy.vo.EditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zbbstart
 * @date 2021/1/18 - 16:48
 */
@RestController
public class EditController {
    @Autowired
    private EditService editService;

    /**
     * 上传编辑页面
     * @param editVo
     * @param res
     * @return
     */
    @RequestMapping(value = "edits", method = RequestMethod.POST)
    public JsonResult addPage(@RequestBody EditVo editVo, HttpServletResponse res) {
        JsonResult result = new JsonResult();
        try {
            boolean b = editService.addPage(editVo);
            if (b) {
                result.setCode(201);
                result.setErrMsg("添加成功");
                res.setStatus(result.getCode());
            } else {
                result.setCode(400);
                result.setErrMsg("添加失败");
                res.setStatus(result.getCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = StateUtils.is500(res);
        }

        return result;
    }

    /**
     * 通过用户id查找编辑的列表
     * @param userId
     * @param res
     * @return
     */
    @RequestMapping(value = "edits/{userId}", method = RequestMethod.GET)
    public JsonResult getAllPages(@PathVariable String userId,HttpServletResponse res){
        JsonResult result = new JsonResult();
        System.out.println("userId:"+userId);
        try {
            List<Page> pages = editService.getPagesByUserId(Integer.parseInt(userId));
            result.setCode(200);
            result.setData(pages);
        } catch (Exception e) {
            e.printStackTrace();
            result = StateUtils.is500(res);
        }
        return result;
    }

    /**
     * 根基页面id查找页面
     * @param id
     * @param res
     * @return
     */
    @RequestMapping(value = "pages/{id}", method = RequestMethod.GET)
    public JsonResult getPageByPageId(@PathVariable String id,HttpServletResponse res){
        JsonResult result = new JsonResult();
        System.out.println("id:"+id);
        try {
            Page page = editService.getPageByPageId(Integer.parseInt(id));
            result.setCode(200);
            result.setData(page);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = StateUtils.is500(res);
        }
        return result;
    }

    /**
     * 根据用户id和
     * @param userId  用户id
     * @param id      页面id
     * @param res     响应
     * @return        JSOn格式
     */
    @RequestMapping(value = "users/{userId}/pages/{id}", method = RequestMethod.DELETE)
    public JsonResult delPageByPageIdAndUserID(@PathVariable("userId") Integer userId ,@PathVariable("id") Integer id,  HttpServletResponse res) {
        JsonResult result = new JsonResult();
        try {
            boolean b = editService.delPageByPageId(id,userId);
            if (b){
                result.setCode(204);
                result.setErrMsg("删除成功...");
            }else {
                result.setCode(400);
                result.setErrMsg("删除失败...");
                res.setStatus(result.getCode());
            }
        } catch (Exception e) {
            result.setCode(500);
            result.setErrMsg("服务器异常");
            res.setStatus(result.getCode());
            e.printStackTrace();
        }
        return result;
    }
}
